package com.itcast.controller;

import com.itcast.pojo.Friendlist;
import com.itcast.pojo.User;
import com.itcast.service.FriendService;
import com.itcast.service.FriendServiceImpl;
import com.itcast.service.UserService;
import com.itcast.service.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

@WebServlet("/InsertFriendsServlet")
public class InsertFriendsServlet extends HttpServlet {

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String friendId = request.getParameter("friendId");
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute("user");
        FriendService friendService = new FriendServiceImpl();
        List<Friendlist> friendlists = friendService.findAllFriendlist(user.getUserId());

            for (Friendlist friendlist1 : friendlists) {

                if (!friendlist1.getUserId().equals(friendId)) {
                    friendService.InsertFriend(user.getUserId(), friendId);
                    break;
                } else {
                    System.out.println("此人已经是你的好友");
                    break;
                }

            }
        
        response.sendRedirect(getServletContext().getContextPath() + "/findFriendServlet");
    }
//        response.sendRedirect(getServletContext().getContextPath() + "/findFriendServlet");
//    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

}
